<#include "/layout/layout.ftl">
<#import "/layout/macro.ftl" as macro>
<#import "/layout/select.ftl" as my/>
<#assign css>
<#--此ftl中用到的css-->
<link rel="stylesheet" href="/assets/vendor/bootstrap-datepicker/css/bootstrap-datepicker3.css">
<link rel="stylesheet" href="/assets/vendor/layui-v2.3.0/css/layui.css">
<style>

    .layui-upload-img{width: 92px; height: 92px; margin: 0 10px 10px 0;}
</style>

</#assign>
<#assign js>
<#--此ftl中用到的js-->

<script src="/assets/vendor/ueditor/ueditor.config.js" ></script>
<script src="/assets/vendor/ueditor/ueditor.all.js" ></script>
<script src="/assets/vendor/bootstrap-datepicker/js/bootstrap-datepicker.js"></script>
<script type="text/javascript" src="/assets/vendor/bootstrap-datepicker/locales/bootstrap-datepicker.zh-CN.min.js" charset="UTF-8"></script>
<script src="/assets/vendor/layui-v2.3.0/layui.js"></script>
<script>
    layui.use('upload', function() {
        var $ = layui.jquery
                , upload = layui.upload;

        //普通图片上传
        var uploadInst = upload.render({
            elem: '#img1'
            , url: '/upload/uploadImg'
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#up_icon').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //如果上传失败
                if (res.code != 0) {
                    return layer.msg('上传失败');
                }
                //上传成功
                layer.msg('上传成功');
                $("#shareImg").val(res.data);
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });
    });
    var ue = UE.getEditor('rule_');
	$(".btn-submit").click(function () {
		// 校验表单合法
		var bootstrapValidator = $(".form-edit").data('bootstrapValidator');
		bootstrapValidator.validate();
		if(bootstrapValidator.isValid()){
			$.ajax({
				type: "POST",
				url: "/invite/edit",
				data: $(".form-edit").serialize(),
				dataType: "JSON",
				success: function(res){
					layer.msg(res.message, {time: 2000
					}, function(){
						location.href="/invite/index";
					});
				}
			});
		}else{
			return false;
		}
	});

	$('.form-edit').bootstrapValidator({
		message: '请输入有效值',
		feedbackIcons: {
			valid: 'glyphicon glyphicon-ok',
			invalid: 'glyphicon glyphicon-remove',
			validating: 'glyphicon glyphicon-refresh'
		}
	});

    $('#beginDate,#endDate').datepicker({
        autoclose: true,
        format:"yyyy-mm-dd",
        todayHighlight:true,
        language:"zh-CN"
    });

    $('#beginDate').unbind("change");
    $('#beginDate').change(function(){
        $('#endDate').datepicker('setStartDate', $("#beginDate").val());
    });
    $('#endDate').unbind("change");
    $('#endDate').change(function(){
        $('#beginDate').datepicker('setEndDate', $("#endDate").val());
    });




</script>
</#assign>
<@layout title="编辑" active="invite">
<!-- Content Header (Page header) -->
<section class="content-header">
	<h1>
		<#if isAdd>
            新增
        <#else>
            编辑
        </#if>
		<small>邀请活动详细信息</small>
	</h1>
	<ol class="breadcrumb">
		<li><a href="/"><i class="fa fa-home"></i> 管理后台</a></li>
		<li><a href="#"><i class="fa fa-list-ul"></i> 邀请活动管理</a></li>
		<li class="active"><i class="fa fa-edit"></i>
		    <#if isAdd>
                新增
            <#else>
                编辑
            </#if>
		</li>
	</ol>
</section>
<!-- Main content -->
<section class="content">
	<div class="row">
		<div class="col-md-12">
			<!-- Default box -->
			<div class="box  box-primary">
				<form class="form-horizontal form-edit" method="post" action="invite/edit">
					<div class="box-body">
						<input type="hidden" id="id" name="id" value="${invite.id!}">
						<input type="hidden" id="isAdd" name="isAdd" value="${isAdd?string('true','false')}">
						<div class="form-group">
							<label class="col-sm-2 control-label">活动标题：</label>
							<div class="col-sm-10">
								<input required placeholder="请输入活动标题" id="title" name="title" class="form-control" type="text" value="${invite.title!}" >
							</div>
						</div>
						<div class="form-group">
							<label class="col-sm-2 control-label">副标题：</label>
							<div class="col-sm-10">
								<input required placeholder="请输入副标题" id="subTitle" name="subTitle" class="form-control" type="text" value="${invite.subTitle!}" >
							</div>
						</div>
						<div class="form-group">
							<label class="col-sm-2 control-label">开始时间：</label>
							<div class="col-sm-10">
								<input required placeholder="请输入开始时间" id="beginDate" name="beginDate" class="form-control" type="text" value="${invite.beginDate!}" >
							</div>
						</div>
						<div class="form-group">
							<label class="col-sm-2 control-label">截止时间：</label>
							<div class="col-sm-10">
								<input required placeholder="请输入截止时间" id="endDate" name="endDate" class="form-control" type="text" value="${invite.endDate!}" >
							</div>
						</div>


                        <div class="form-group">
                            <label class="col-sm-2 control-label">活动链接：</label>
                            <div class="col-sm-10">
                                <input required placeholder="请输入活动链接" id="url" name="url" class="form-control" value="${invite.url!}" >
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">是否立即启用：</label>
                            <div class="col-sm-10">
							    <@my.select id="isAvailable" datas=getDict('IS_NOT')  value="${invite.isAvailable!}" defaultValue="请选择"/>
                            </div>
                        </div>
						<div class="form-group">
							<label class="col-sm-2 control-label">新用户获得的优惠券：</label>
							<#--<div class="col-sm-10">
								<input required placeholder="请输入发放的优惠券 多个优惠券','隔开" id="coupons" name="coupons" class="form-control" type="text" value="${invite.coupons!}" >
							</div>-->

                            <div class="col-sm-10">
                                <select id="coupons" name="coupons" class="form-control">
                                    <option value="">请选择新用户获得的优惠券</option>
									<#list coupons as coupon>
										<option value="${coupon.id}" data-start="${coupon.id}">${coupon.name+'(优惠金额'+coupon.amount+')'}</option>
									</#list>
                                </select>
                            </div>

						</div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">邀请用户获得的优惠券：</label>

                            <div class="col-sm-10">
                                <select id="inviteCoupons" name="inviteCoupons" class="form-control">
                                    <option value="">请选择邀请用户获得的优惠券</option>
									<#list coupons as coupon>
										<option value="${coupon.id}" data-start="${coupon.id}">${coupon.name+'(优惠金额'+coupon.amount+')'}</option>
									</#list>
                                </select>
                            </div>

                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">分享后显示的图片：</label>
                            <div class="col-sm-10">
                                <input type="hidden" name="shareImg" id="shareImg" value="${invite.shareImg!}">
                                <div class="layui-upload">
                                    <button type="button" class="layui-btn" id="img1">上传图片</button>
                                    <div class="layui-upload-list">
                                        <img class="layui-upload-img" id="up_icon" src="/${invite.shareImg!}">
                                        <p id="demoText"></p>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">活动规则：</label>
                            <div class="col-sm-10">
                                <#--<div id="rule" name="rule">${invite.rule!}</div>-->

                                <script id="rule_" name="rule" type="text/plain">
									${invite.rule!}
								</script>

                            </div>
                        </div>

					<div class="box-footer">
						<button type="button" class="btn btn-default btn-back">返回</button>
						<button type="button" class="btn btn-info pull-right btn-submit">提交</button>
					</div>

				</form>
			</div>
			<!-- /.box -->
		</div>
	</div>
</section>
<!-- /.content -->
</@layout>
